Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
| 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 George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 27 | #include "py/builtin.h" |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 28 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 29 | #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 30 | |
Damien George | 51dfcb4 | 2015-01-01 20:27:54 +0000 | [diff] [blame] | 31 | #include <math.h> |
| 32 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 33 | /// \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 George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 38 | // These are defined in modmath.c |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 39 | /// \constant e - base of the natural logarithm |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 40 | extern const mp_obj_float_t mp_math_e_obj; |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 41 | /// \constant pi - the ratio of a circle's circumference to its diameter |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 42 | extern const mp_obj_float_t mp_math_pi_obj; |
| 43 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 44 | /// \function phase(z) |
| 45 | /// Returns the phase of the number `z`, in the range (-pi, +pi]. |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 46 | STATIC mp_obj_t mp_cmath_phase(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 47 | mp_float_t real, imag; |
| 48 | mp_obj_get_complex(z_obj, &real, &imag); |
| 49 | return mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real)); |
| 50 | } |
| 51 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_phase_obj, mp_cmath_phase); |
| 52 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 53 | /// \function polar(z) |
| 54 | /// Returns, as a tuple, the polar form of `z`. |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 55 | STATIC mp_obj_t mp_cmath_polar(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 56 | mp_float_t real, imag; |
| 57 | mp_obj_get_complex(z_obj, &real, &imag); |
| 58 | mp_obj_t tuple[2] = { |
| 59 | mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real*real + imag*imag)), |
| 60 | mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real)), |
| 61 | }; |
| 62 | return mp_obj_new_tuple(2, tuple); |
| 63 | } |
| 64 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_polar_obj, mp_cmath_polar); |
| 65 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 66 | /// \function rect(r, phi) |
Damien George | 7703d71 | 2014-08-11 22:19:44 +0000 | [diff] [blame] | 67 | /// Returns the complex number with modulus `r` and phase `phi`. |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 68 | STATIC mp_obj_t mp_cmath_rect(mp_obj_t r_obj, mp_obj_t phi_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 69 | mp_float_t r = mp_obj_get_float(r_obj); |
| 70 | mp_float_t phi = mp_obj_get_float(phi_obj); |
| 71 | return mp_obj_new_complex(r * MICROPY_FLOAT_C_FUN(cos)(phi), r * MICROPY_FLOAT_C_FUN(sin)(phi)); |
| 72 | } |
| 73 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(mp_cmath_rect_obj, mp_cmath_rect); |
| 74 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 75 | /// \function exp(z) |
Damien George | 7703d71 | 2014-08-11 22:19:44 +0000 | [diff] [blame] | 76 | /// Return the exponential of `z`. |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 77 | STATIC mp_obj_t mp_cmath_exp(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 78 | mp_float_t real, imag; |
| 79 | mp_obj_get_complex(z_obj, &real, &imag); |
| 80 | mp_float_t exp_real = MICROPY_FLOAT_C_FUN(exp)(real); |
| 81 | return mp_obj_new_complex(exp_real * MICROPY_FLOAT_C_FUN(cos)(imag), exp_real * MICROPY_FLOAT_C_FUN(sin)(imag)); |
| 82 | } |
| 83 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_exp_obj, mp_cmath_exp); |
| 84 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 85 | /// \function log(z) |
Damien George | 7703d71 | 2014-08-11 22:19:44 +0000 | [diff] [blame] | 86 | /// Return the natural logarithm of `z`. The branch cut is along the negative real axis. |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 87 | // TODO can take second argument, being the base |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 88 | STATIC mp_obj_t mp_cmath_log(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 89 | mp_float_t real, imag; |
| 90 | mp_obj_get_complex(z_obj, &real, &imag); |
| 91 | return mp_obj_new_complex(0.5 * MICROPY_FLOAT_C_FUN(log)(real*real + imag*imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real)); |
| 92 | } |
| 93 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log_obj, mp_cmath_log); |
| 94 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 95 | /// \function log10(z) |
Damien George | 7703d71 | 2014-08-11 22:19:44 +0000 | [diff] [blame] | 96 | /// Return the base-10 logarithm of `z`. The branch cut is along the negative real axis. |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 97 | STATIC mp_obj_t mp_cmath_log10(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 98 | mp_float_t real, imag; |
| 99 | mp_obj_get_complex(z_obj, &real, &imag); |
Damien George | f49782f | 2015-02-02 12:52:14 +0000 | [diff] [blame^] | 100 | 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 George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 101 | } |
| 102 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log10_obj, mp_cmath_log10); |
| 103 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 104 | /// \function sqrt(z) |
Damien George | 7703d71 | 2014-08-11 22:19:44 +0000 | [diff] [blame] | 105 | /// Return the square-root of `z`. |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 106 | STATIC mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 107 | mp_float_t real, imag; |
| 108 | mp_obj_get_complex(z_obj, &real, &imag); |
| 109 | mp_float_t sqrt_abs = MICROPY_FLOAT_C_FUN(pow)(real*real + imag*imag, 0.25); |
| 110 | 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] | 111 | 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] | 112 | } |
| 113 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sqrt_obj, mp_cmath_sqrt); |
| 114 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 115 | /// \function cos(z) |
Damien George | 7703d71 | 2014-08-11 22:19:44 +0000 | [diff] [blame] | 116 | /// Return the cosine of `z`. |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 117 | STATIC mp_obj_t mp_cmath_cos(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 118 | mp_float_t real, imag; |
| 119 | mp_obj_get_complex(z_obj, &real, &imag); |
| 120 | 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)); |
| 121 | } |
| 122 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_cos_obj, mp_cmath_cos); |
| 123 | |
Damien George | 30dd23a | 2014-08-10 17:50:28 +0100 | [diff] [blame] | 124 | /// \function sin(z) |
Damien George | 7703d71 | 2014-08-11 22:19:44 +0000 | [diff] [blame] | 125 | /// Return the sine of `z`. |
Damien George | 969a6b3 | 2014-12-10 22:07:04 +0000 | [diff] [blame] | 126 | STATIC mp_obj_t mp_cmath_sin(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 127 | mp_float_t real, imag; |
| 128 | mp_obj_get_complex(z_obj, &real, &imag); |
| 129 | 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)); |
| 130 | } |
| 131 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sin_obj, mp_cmath_sin); |
| 132 | |
| 133 | STATIC const mp_map_elem_t mp_module_cmath_globals_table[] = { |
| 134 | { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_cmath) }, |
| 135 | { MP_OBJ_NEW_QSTR(MP_QSTR_e), (mp_obj_t)&mp_math_e_obj }, |
| 136 | { MP_OBJ_NEW_QSTR(MP_QSTR_pi), (mp_obj_t)&mp_math_pi_obj }, |
| 137 | { MP_OBJ_NEW_QSTR(MP_QSTR_phase), (mp_obj_t)&mp_cmath_phase_obj }, |
| 138 | { MP_OBJ_NEW_QSTR(MP_QSTR_polar), (mp_obj_t)&mp_cmath_polar_obj }, |
| 139 | { MP_OBJ_NEW_QSTR(MP_QSTR_rect), (mp_obj_t)&mp_cmath_rect_obj }, |
| 140 | { MP_OBJ_NEW_QSTR(MP_QSTR_exp), (mp_obj_t)&mp_cmath_exp_obj }, |
| 141 | { MP_OBJ_NEW_QSTR(MP_QSTR_log), (mp_obj_t)&mp_cmath_log_obj }, |
| 142 | { MP_OBJ_NEW_QSTR(MP_QSTR_log10), (mp_obj_t)&mp_cmath_log10_obj }, |
| 143 | { MP_OBJ_NEW_QSTR(MP_QSTR_sqrt), (mp_obj_t)&mp_cmath_sqrt_obj }, |
| 144 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_acos), (mp_obj_t)&mp_cmath_acos_obj }, |
| 145 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_asin), (mp_obj_t)&mp_cmath_asin_obj }, |
| 146 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_atan), (mp_obj_t)&mp_cmath_atan_obj }, |
| 147 | { MP_OBJ_NEW_QSTR(MP_QSTR_cos), (mp_obj_t)&mp_cmath_cos_obj }, |
| 148 | { MP_OBJ_NEW_QSTR(MP_QSTR_sin), (mp_obj_t)&mp_cmath_sin_obj }, |
| 149 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_tan), (mp_obj_t)&mp_cmath_tan_obj }, |
| 150 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_acosh), (mp_obj_t)&mp_cmath_acosh_obj }, |
| 151 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_asinh), (mp_obj_t)&mp_cmath_asinh_obj }, |
| 152 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_atanh), (mp_obj_t)&mp_cmath_atanh_obj }, |
| 153 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_cosh), (mp_obj_t)&mp_cmath_cosh_obj }, |
| 154 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_sinh), (mp_obj_t)&mp_cmath_sinh_obj }, |
| 155 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_tanh), (mp_obj_t)&mp_cmath_tanh_obj }, |
| 156 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_isfinite), (mp_obj_t)&mp_cmath_isfinite_obj }, |
| 157 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_isinf), (mp_obj_t)&mp_cmath_isinf_obj }, |
| 158 | //{ MP_OBJ_NEW_QSTR(MP_QSTR_isnan), (mp_obj_t)&mp_cmath_isnan_obj }, |
| 159 | }; |
| 160 | |
Damien George | 3b603f2 | 2014-11-29 14:39:27 +0000 | [diff] [blame] | 161 | STATIC MP_DEFINE_CONST_DICT(mp_module_cmath_globals, mp_module_cmath_globals_table); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 162 | |
| 163 | const mp_obj_module_t mp_module_cmath = { |
| 164 | .base = { &mp_type_module }, |
| 165 | .name = MP_QSTR_cmath, |
| 166 | .globals = (mp_obj_dict_t*)&mp_module_cmath_globals, |
| 167 | }; |
| 168 | |
Damien George | fb510b3 | 2014-06-01 13:32:54 +0100 | [diff] [blame] | 169 | #endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_CMATH |