Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Damien George | 04b9147 | 2014-05-03 23:27:38 +0100 | [diff] [blame] | 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 | |
Paul Sokolovsky | 2b7236d | 2015-12-07 00:18:44 +0200 | [diff] [blame] | 29 | #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_BUILTINS_COMPLEX && 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 | 0102ee0 | 2017-08-30 21:02:00 +1000 | [diff] [blame] | 33 | // phase(z): returns the phase of the number z in the range (-pi, +pi] |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 34 | static mp_obj_t mp_cmath_phase(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 35 | mp_float_t real, imag; |
| 36 | mp_obj_get_complex(z_obj, &real, &imag); |
| 37 | return mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real)); |
| 38 | } |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 39 | static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_phase_obj, mp_cmath_phase); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 40 | |
Damien George | 0102ee0 | 2017-08-30 21:02:00 +1000 | [diff] [blame] | 41 | // polar(z): returns the polar form of z as a tuple |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 42 | static mp_obj_t mp_cmath_polar(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 43 | mp_float_t real, imag; |
| 44 | mp_obj_get_complex(z_obj, &real, &imag); |
| 45 | mp_obj_t tuple[2] = { |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 46 | mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real * real + imag * imag)), |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 47 | mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real)), |
| 48 | }; |
| 49 | return mp_obj_new_tuple(2, tuple); |
| 50 | } |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 51 | static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_polar_obj, mp_cmath_polar); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 52 | |
Damien George | 0102ee0 | 2017-08-30 21:02:00 +1000 | [diff] [blame] | 53 | // rect(r, phi): returns the complex number with modulus r and phase phi |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 54 | 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] | 55 | mp_float_t r = mp_obj_get_float(r_obj); |
| 56 | mp_float_t phi = mp_obj_get_float(phi_obj); |
| 57 | return mp_obj_new_complex(r * MICROPY_FLOAT_C_FUN(cos)(phi), r * MICROPY_FLOAT_C_FUN(sin)(phi)); |
| 58 | } |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 59 | static MP_DEFINE_CONST_FUN_OBJ_2(mp_cmath_rect_obj, mp_cmath_rect); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 60 | |
Damien George | 0102ee0 | 2017-08-30 21:02:00 +1000 | [diff] [blame] | 61 | // exp(z): return the exponential of z |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 62 | static mp_obj_t mp_cmath_exp(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 63 | mp_float_t real, imag; |
| 64 | mp_obj_get_complex(z_obj, &real, &imag); |
| 65 | mp_float_t exp_real = MICROPY_FLOAT_C_FUN(exp)(real); |
| 66 | return mp_obj_new_complex(exp_real * MICROPY_FLOAT_C_FUN(cos)(imag), exp_real * MICROPY_FLOAT_C_FUN(sin)(imag)); |
| 67 | } |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 68 | static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_exp_obj, mp_cmath_exp); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 69 | |
Damien George | 0102ee0 | 2017-08-30 21:02:00 +1000 | [diff] [blame] | 70 | // log(z): return the natural logarithm of z, with branch cut along the negative real axis |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 71 | // TODO can take second argument, being the base |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 72 | static mp_obj_t mp_cmath_log(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 73 | mp_float_t real, imag; |
| 74 | mp_obj_get_complex(z_obj, &real, &imag); |
stijn | 0ba68f8 | 2020-04-09 09:05:48 +0200 | [diff] [blame] | 75 | return mp_obj_new_complex(MICROPY_FLOAT_CONST(0.5) * MICROPY_FLOAT_C_FUN(log)(real * real + imag * imag), MICROPY_FLOAT_C_FUN(atan2)(imag, real)); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 76 | } |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 77 | static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log_obj, mp_cmath_log); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 78 | |
Damien George | a24eafa | 2015-11-17 14:10:13 +0000 | [diff] [blame] | 79 | #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS |
Damien George | 0102ee0 | 2017-08-30 21:02:00 +1000 | [diff] [blame] | 80 | // log10(z): return the base-10 logarithm of z, with branch cut along the negative real axis |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 81 | static mp_obj_t mp_cmath_log10(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 82 | mp_float_t real, imag; |
| 83 | mp_obj_get_complex(z_obj, &real, &imag); |
stijn | 0ba68f8 | 2020-04-09 09:05:48 +0200 | [diff] [blame] | 84 | return mp_obj_new_complex(MICROPY_FLOAT_CONST(0.5) * MICROPY_FLOAT_C_FUN(log10)(real * real + imag * imag), MICROPY_FLOAT_CONST(0.4342944819032518) * MICROPY_FLOAT_C_FUN(atan2)(imag, real)); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 85 | } |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 86 | static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log10_obj, mp_cmath_log10); |
Damien George | a24eafa | 2015-11-17 14:10:13 +0000 | [diff] [blame] | 87 | #endif |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 88 | |
Damien George | 0102ee0 | 2017-08-30 21:02:00 +1000 | [diff] [blame] | 89 | // sqrt(z): return the square-root of z |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 90 | static mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 91 | mp_float_t real, imag; |
| 92 | mp_obj_get_complex(z_obj, &real, &imag); |
stijn | 0ba68f8 | 2020-04-09 09:05:48 +0200 | [diff] [blame] | 93 | mp_float_t sqrt_abs = MICROPY_FLOAT_C_FUN(pow)(real * real + imag * imag, MICROPY_FLOAT_CONST(0.25)); |
| 94 | mp_float_t theta = MICROPY_FLOAT_CONST(0.5) * MICROPY_FLOAT_C_FUN(atan2)(imag, real); |
Damien George | 93e51b5 | 2014-05-03 18:40:50 +0100 | [diff] [blame] | 95 | 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] | 96 | } |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 97 | static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sqrt_obj, mp_cmath_sqrt); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 98 | |
Damien George | 0102ee0 | 2017-08-30 21:02:00 +1000 | [diff] [blame] | 99 | // cos(z): return the cosine of z |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 100 | static mp_obj_t mp_cmath_cos(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 101 | mp_float_t real, imag; |
| 102 | mp_obj_get_complex(z_obj, &real, &imag); |
| 103 | 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)); |
| 104 | } |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 105 | static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_cos_obj, mp_cmath_cos); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 106 | |
Damien George | 0102ee0 | 2017-08-30 21:02:00 +1000 | [diff] [blame] | 107 | // sin(z): return the sine of z |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 108 | static mp_obj_t mp_cmath_sin(mp_obj_t z_obj) { |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 109 | mp_float_t real, imag; |
| 110 | mp_obj_get_complex(z_obj, &real, &imag); |
| 111 | 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)); |
| 112 | } |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 113 | static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sin_obj, mp_cmath_sin); |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 114 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 115 | static const mp_rom_map_elem_t mp_module_cmath_globals_table[] = { |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 116 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cmath) }, |
| 117 | { MP_ROM_QSTR(MP_QSTR_e), mp_const_float_e }, |
| 118 | { MP_ROM_QSTR(MP_QSTR_pi), mp_const_float_pi }, |
| 119 | { MP_ROM_QSTR(MP_QSTR_phase), MP_ROM_PTR(&mp_cmath_phase_obj) }, |
| 120 | { MP_ROM_QSTR(MP_QSTR_polar), MP_ROM_PTR(&mp_cmath_polar_obj) }, |
| 121 | { MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&mp_cmath_rect_obj) }, |
| 122 | { MP_ROM_QSTR(MP_QSTR_exp), MP_ROM_PTR(&mp_cmath_exp_obj) }, |
| 123 | { MP_ROM_QSTR(MP_QSTR_log), MP_ROM_PTR(&mp_cmath_log_obj) }, |
Damien George | a24eafa | 2015-11-17 14:10:13 +0000 | [diff] [blame] | 124 | #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 125 | { MP_ROM_QSTR(MP_QSTR_log10), MP_ROM_PTR(&mp_cmath_log10_obj) }, |
Damien George | a24eafa | 2015-11-17 14:10:13 +0000 | [diff] [blame] | 126 | #endif |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 127 | { MP_ROM_QSTR(MP_QSTR_sqrt), MP_ROM_PTR(&mp_cmath_sqrt_obj) }, |
stijn | 84fa331 | 2020-04-16 09:13:57 +0200 | [diff] [blame] | 128 | // { MP_ROM_QSTR(MP_QSTR_acos), MP_ROM_PTR(&mp_cmath_acos_obj) }, |
| 129 | // { MP_ROM_QSTR(MP_QSTR_asin), MP_ROM_PTR(&mp_cmath_asin_obj) }, |
| 130 | // { MP_ROM_QSTR(MP_QSTR_atan), MP_ROM_PTR(&mp_cmath_atan_obj) }, |
Damien George | cbf7674 | 2015-11-27 13:38:15 +0000 | [diff] [blame] | 131 | { MP_ROM_QSTR(MP_QSTR_cos), MP_ROM_PTR(&mp_cmath_cos_obj) }, |
| 132 | { MP_ROM_QSTR(MP_QSTR_sin), MP_ROM_PTR(&mp_cmath_sin_obj) }, |
stijn | 84fa331 | 2020-04-16 09:13:57 +0200 | [diff] [blame] | 133 | // { MP_ROM_QSTR(MP_QSTR_tan), MP_ROM_PTR(&mp_cmath_tan_obj) }, |
| 134 | // { MP_ROM_QSTR(MP_QSTR_acosh), MP_ROM_PTR(&mp_cmath_acosh_obj) }, |
| 135 | // { MP_ROM_QSTR(MP_QSTR_asinh), MP_ROM_PTR(&mp_cmath_asinh_obj) }, |
| 136 | // { MP_ROM_QSTR(MP_QSTR_atanh), MP_ROM_PTR(&mp_cmath_atanh_obj) }, |
| 137 | // { MP_ROM_QSTR(MP_QSTR_cosh), MP_ROM_PTR(&mp_cmath_cosh_obj) }, |
| 138 | // { MP_ROM_QSTR(MP_QSTR_sinh), MP_ROM_PTR(&mp_cmath_sinh_obj) }, |
| 139 | // { MP_ROM_QSTR(MP_QSTR_tanh), MP_ROM_PTR(&mp_cmath_tanh_obj) }, |
| 140 | // { MP_ROM_QSTR(MP_QSTR_isfinite), MP_ROM_PTR(&mp_cmath_isfinite_obj) }, |
| 141 | // { MP_ROM_QSTR(MP_QSTR_isinf), MP_ROM_PTR(&mp_cmath_isinf_obj) }, |
| 142 | // { MP_ROM_QSTR(MP_QSTR_isnan), MP_ROM_PTR(&mp_cmath_isnan_obj) }, |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 143 | }; |
| 144 | |
Angus Gratton | decf8e6 | 2024-02-27 15:32:29 +1100 | [diff] [blame] | 145 | 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] | 146 | |
| 147 | const mp_obj_module_t mp_module_cmath = { |
| 148 | .base = { &mp_type_module }, |
Damien George | 69661f3 | 2020-02-27 15:36:53 +1100 | [diff] [blame] | 149 | .globals = (mp_obj_dict_t *)&mp_module_cmath_globals, |
Damien George | dbdfee1 | 2014-04-17 17:11:03 +0100 | [diff] [blame] | 150 | }; |
| 151 | |
Damien George | efe23ac | 2022-05-31 22:56:11 +1000 | [diff] [blame] | 152 | MP_REGISTER_MODULE(MP_QSTR_cmath, mp_module_cmath); |
Jim Mussared | d8d3e6a | 2022-04-20 16:14:22 +1000 | [diff] [blame] | 153 | |
| 154 | #endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_BUILTINS_COMPLEX && MICROPY_PY_CMATH |