modcmath.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * This file is part of the MicroPython 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. #include "py/builtin.h"
  27. #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_BUILTINS_COMPLEX && MICROPY_PY_CMATH
  28. #include <math.h>
  29. // phase(z): returns the phase of the number z in the range (-pi, +pi]
  30. static mp_obj_t mp_cmath_phase(mp_obj_t z_obj) {
  31. mp_float_t real, imag;
  32. mp_obj_get_complex(z_obj, &real, &imag);
  33. return mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real));
  34. }
  35. static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_phase_obj, mp_cmath_phase);
  36. // polar(z): returns the polar form of z as a tuple
  37. static mp_obj_t mp_cmath_polar(mp_obj_t z_obj) {
  38. mp_float_t real, imag;
  39. mp_obj_get_complex(z_obj, &real, &imag);
  40. mp_obj_t tuple[2] = {
  41. mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(real * real + imag * imag)),
  42. mp_obj_new_float(MICROPY_FLOAT_C_FUN(atan2)(imag, real)),
  43. };
  44. return mp_obj_new_tuple(2, tuple);
  45. }
  46. static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_polar_obj, mp_cmath_polar);
  47. // rect(r, phi): returns the complex number with modulus r and phase phi
  48. static mp_obj_t mp_cmath_rect(mp_obj_t r_obj, mp_obj_t phi_obj) {
  49. mp_float_t r = mp_obj_get_float(r_obj);
  50. mp_float_t phi = mp_obj_get_float(phi_obj);
  51. return mp_obj_new_complex(r * MICROPY_FLOAT_C_FUN(cos)(phi), r * MICROPY_FLOAT_C_FUN(sin)(phi));
  52. }
  53. static MP_DEFINE_CONST_FUN_OBJ_2(mp_cmath_rect_obj, mp_cmath_rect);
  54. // exp(z): return the exponential of z
  55. static mp_obj_t mp_cmath_exp(mp_obj_t z_obj) {
  56. mp_float_t real, imag;
  57. mp_obj_get_complex(z_obj, &real, &imag);
  58. mp_float_t exp_real = MICROPY_FLOAT_C_FUN(exp)(real);
  59. return mp_obj_new_complex(exp_real * MICROPY_FLOAT_C_FUN(cos)(imag), exp_real * MICROPY_FLOAT_C_FUN(sin)(imag));
  60. }
  61. static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_exp_obj, mp_cmath_exp);
  62. // log(z): return the natural logarithm of z, with branch cut along the negative real axis
  63. // TODO can take second argument, being the base
  64. static mp_obj_t mp_cmath_log(mp_obj_t z_obj) {
  65. mp_float_t real, imag;
  66. mp_obj_get_complex(z_obj, &real, &imag);
  67. 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));
  68. }
  69. static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log_obj, mp_cmath_log);
  70. #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
  71. // log10(z): return the base-10 logarithm of z, with branch cut along the negative real axis
  72. static mp_obj_t mp_cmath_log10(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_CONST(0.5) * MICROPY_FLOAT_C_FUN(log10)(real * real + imag * imag), MICROPY_FLOAT_CONST(0.4342944819032518) * MICROPY_FLOAT_C_FUN(atan2)(imag, real));
  76. }
  77. static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_log10_obj, mp_cmath_log10);
  78. #endif
  79. // sqrt(z): return the square-root of z
  80. static mp_obj_t mp_cmath_sqrt(mp_obj_t z_obj) {
  81. mp_float_t real, imag;
  82. mp_obj_get_complex(z_obj, &real, &imag);
  83. mp_float_t sqrt_abs = MICROPY_FLOAT_C_FUN(pow)(real * real + imag * imag, MICROPY_FLOAT_CONST(0.25));
  84. mp_float_t theta = MICROPY_FLOAT_CONST(0.5) * MICROPY_FLOAT_C_FUN(atan2)(imag, real);
  85. return mp_obj_new_complex(sqrt_abs * MICROPY_FLOAT_C_FUN(cos)(theta), sqrt_abs * MICROPY_FLOAT_C_FUN(sin)(theta));
  86. }
  87. static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sqrt_obj, mp_cmath_sqrt);
  88. // cos(z): return the cosine of z
  89. static mp_obj_t mp_cmath_cos(mp_obj_t z_obj) {
  90. mp_float_t real, imag;
  91. mp_obj_get_complex(z_obj, &real, &imag);
  92. 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));
  93. }
  94. static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_cos_obj, mp_cmath_cos);
  95. // sin(z): return the sine of z
  96. static mp_obj_t mp_cmath_sin(mp_obj_t z_obj) {
  97. mp_float_t real, imag;
  98. mp_obj_get_complex(z_obj, &real, &imag);
  99. 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));
  100. }
  101. static MP_DEFINE_CONST_FUN_OBJ_1(mp_cmath_sin_obj, mp_cmath_sin);
  102. static const mp_rom_map_elem_t mp_module_cmath_globals_table[] = {
  103. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_cmath) },
  104. { MP_ROM_QSTR(MP_QSTR_e), mp_const_float_e },
  105. { MP_ROM_QSTR(MP_QSTR_pi), mp_const_float_pi },
  106. { MP_ROM_QSTR(MP_QSTR_phase), MP_ROM_PTR(&mp_cmath_phase_obj) },
  107. { MP_ROM_QSTR(MP_QSTR_polar), MP_ROM_PTR(&mp_cmath_polar_obj) },
  108. { MP_ROM_QSTR(MP_QSTR_rect), MP_ROM_PTR(&mp_cmath_rect_obj) },
  109. { MP_ROM_QSTR(MP_QSTR_exp), MP_ROM_PTR(&mp_cmath_exp_obj) },
  110. { MP_ROM_QSTR(MP_QSTR_log), MP_ROM_PTR(&mp_cmath_log_obj) },
  111. #if MICROPY_PY_MATH_SPECIAL_FUNCTIONS
  112. { MP_ROM_QSTR(MP_QSTR_log10), MP_ROM_PTR(&mp_cmath_log10_obj) },
  113. #endif
  114. { MP_ROM_QSTR(MP_QSTR_sqrt), MP_ROM_PTR(&mp_cmath_sqrt_obj) },
  115. // { MP_ROM_QSTR(MP_QSTR_acos), MP_ROM_PTR(&mp_cmath_acos_obj) },
  116. // { MP_ROM_QSTR(MP_QSTR_asin), MP_ROM_PTR(&mp_cmath_asin_obj) },
  117. // { MP_ROM_QSTR(MP_QSTR_atan), MP_ROM_PTR(&mp_cmath_atan_obj) },
  118. { MP_ROM_QSTR(MP_QSTR_cos), MP_ROM_PTR(&mp_cmath_cos_obj) },
  119. { MP_ROM_QSTR(MP_QSTR_sin), MP_ROM_PTR(&mp_cmath_sin_obj) },
  120. // { MP_ROM_QSTR(MP_QSTR_tan), MP_ROM_PTR(&mp_cmath_tan_obj) },
  121. // { MP_ROM_QSTR(MP_QSTR_acosh), MP_ROM_PTR(&mp_cmath_acosh_obj) },
  122. // { MP_ROM_QSTR(MP_QSTR_asinh), MP_ROM_PTR(&mp_cmath_asinh_obj) },
  123. // { MP_ROM_QSTR(MP_QSTR_atanh), MP_ROM_PTR(&mp_cmath_atanh_obj) },
  124. // { MP_ROM_QSTR(MP_QSTR_cosh), MP_ROM_PTR(&mp_cmath_cosh_obj) },
  125. // { MP_ROM_QSTR(MP_QSTR_sinh), MP_ROM_PTR(&mp_cmath_sinh_obj) },
  126. // { MP_ROM_QSTR(MP_QSTR_tanh), MP_ROM_PTR(&mp_cmath_tanh_obj) },
  127. // { MP_ROM_QSTR(MP_QSTR_isfinite), MP_ROM_PTR(&mp_cmath_isfinite_obj) },
  128. // { MP_ROM_QSTR(MP_QSTR_isinf), MP_ROM_PTR(&mp_cmath_isinf_obj) },
  129. // { MP_ROM_QSTR(MP_QSTR_isnan), MP_ROM_PTR(&mp_cmath_isnan_obj) },
  130. };
  131. static MP_DEFINE_CONST_DICT(mp_module_cmath_globals, mp_module_cmath_globals_table);
  132. const mp_obj_module_t mp_module_cmath = {
  133. .base = { &mp_type_module },
  134. .globals = (mp_obj_dict_t *)&mp_module_cmath_globals,
  135. };
  136. MP_REGISTER_MODULE(MP_QSTR_cmath, mp_module_cmath);
  137. #endif // MICROPY_PY_BUILTINS_FLOAT && MICROPY_PY_BUILTINS_COMPLEX && MICROPY_PY_CMATH