objcomplex.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <stdlib.h>
  27. #include <stdio.h>
  28. #include <assert.h>
  29. #include "py/parsenum.h"
  30. #include "py/runtime.h"
  31. #if MICROPY_PY_BUILTINS_COMPLEX
  32. #include <math.h>
  33. #include "py/formatfloat.h"
  34. typedef struct _mp_obj_complex_t {
  35. mp_obj_base_t base;
  36. mp_float_t real;
  37. mp_float_t imag;
  38. } mp_obj_complex_t;
  39. static void complex_print(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t kind) {
  40. (void)kind;
  41. mp_obj_complex_t *o = MP_OBJ_TO_PTR(o_in);
  42. #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
  43. char buf[16];
  44. #if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_C
  45. const int precision = 6;
  46. #else
  47. const int precision = 7;
  48. #endif
  49. #else
  50. char buf[32];
  51. const int precision = 16;
  52. #endif
  53. if (o->real == 0) {
  54. mp_format_float(o->imag, buf, sizeof(buf), 'g', precision, '\0');
  55. mp_printf(print, "%sj", buf);
  56. } else {
  57. mp_format_float(o->real, buf, sizeof(buf), 'g', precision, '\0');
  58. mp_printf(print, "(%s", buf);
  59. if (o->imag >= 0 || isnan(o->imag)) {
  60. mp_print_str(print, "+");
  61. }
  62. mp_format_float(o->imag, buf, sizeof(buf), 'g', precision, '\0');
  63. mp_printf(print, "%sj)", buf);
  64. }
  65. }
  66. static mp_obj_t complex_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
  67. (void)type_in;
  68. mp_arg_check_num(n_args, n_kw, 0, 2, false);
  69. switch (n_args) {
  70. case 0:
  71. return mp_obj_new_complex(0, 0);
  72. case 1:
  73. if (mp_obj_is_str(args[0])) {
  74. // a string, parse it
  75. size_t l;
  76. const char *s = mp_obj_str_get_data(args[0], &l);
  77. return mp_parse_num_complex(s, l, NULL);
  78. } else if (mp_obj_is_type(args[0], &mp_type_complex)) {
  79. // a complex, just return it
  80. return args[0];
  81. } else {
  82. mp_float_t real, imag;
  83. mp_obj_get_complex(args[0], &real, &imag);
  84. return mp_obj_new_complex(real, imag);
  85. }
  86. case 2:
  87. default: {
  88. mp_float_t real, imag;
  89. if (mp_obj_is_type(args[0], &mp_type_complex)) {
  90. mp_obj_complex_get(args[0], &real, &imag);
  91. } else {
  92. real = mp_obj_get_float(args[0]);
  93. imag = 0;
  94. }
  95. if (mp_obj_is_type(args[1], &mp_type_complex)) {
  96. mp_float_t real2, imag2;
  97. mp_obj_complex_get(args[1], &real2, &imag2);
  98. real -= imag2;
  99. imag += real2;
  100. } else {
  101. imag += mp_obj_get_float(args[1]);
  102. }
  103. return mp_obj_new_complex(real, imag);
  104. }
  105. }
  106. }
  107. static mp_obj_t complex_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
  108. mp_obj_complex_t *o = MP_OBJ_TO_PTR(o_in);
  109. switch (op) {
  110. case MP_UNARY_OP_BOOL:
  111. return mp_obj_new_bool(o->real != 0 || o->imag != 0);
  112. case MP_UNARY_OP_HASH:
  113. return MP_OBJ_NEW_SMALL_INT(mp_float_hash(o->real) ^ mp_float_hash(o->imag));
  114. case MP_UNARY_OP_POSITIVE:
  115. return o_in;
  116. case MP_UNARY_OP_NEGATIVE:
  117. return mp_obj_new_complex(-o->real, -o->imag);
  118. case MP_UNARY_OP_ABS:
  119. return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(o->real * o->real + o->imag * o->imag));
  120. default:
  121. return MP_OBJ_NULL; // op not supported
  122. }
  123. }
  124. static mp_obj_t complex_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
  125. mp_obj_complex_t *lhs = MP_OBJ_TO_PTR(lhs_in);
  126. return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
  127. }
  128. static void complex_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  129. if (dest[0] != MP_OBJ_NULL) {
  130. // not load attribute
  131. return;
  132. }
  133. mp_obj_complex_t *self = MP_OBJ_TO_PTR(self_in);
  134. if (attr == MP_QSTR_real) {
  135. dest[0] = mp_obj_new_float(self->real);
  136. } else if (attr == MP_QSTR_imag) {
  137. dest[0] = mp_obj_new_float(self->imag);
  138. }
  139. }
  140. MP_DEFINE_CONST_OBJ_TYPE(
  141. mp_type_complex, MP_QSTR_complex, MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE,
  142. make_new, complex_make_new,
  143. print, complex_print,
  144. unary_op, complex_unary_op,
  145. binary_op, complex_binary_op,
  146. attr, complex_attr
  147. );
  148. mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
  149. mp_obj_complex_t *o = mp_obj_malloc(mp_obj_complex_t, &mp_type_complex);
  150. o->real = real;
  151. o->imag = imag;
  152. return MP_OBJ_FROM_PTR(o);
  153. }
  154. void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
  155. assert(mp_obj_is_type(self_in, &mp_type_complex));
  156. mp_obj_complex_t *self = MP_OBJ_TO_PTR(self_in);
  157. *real = self->real;
  158. *imag = self->imag;
  159. }
  160. mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) {
  161. mp_float_t rhs_real, rhs_imag;
  162. if (!mp_obj_get_complex_maybe(rhs_in, &rhs_real, &rhs_imag)) {
  163. return MP_OBJ_NULL; // op not supported
  164. }
  165. switch (op) {
  166. case MP_BINARY_OP_ADD:
  167. case MP_BINARY_OP_INPLACE_ADD:
  168. lhs_real += rhs_real;
  169. lhs_imag += rhs_imag;
  170. break;
  171. case MP_BINARY_OP_SUBTRACT:
  172. case MP_BINARY_OP_INPLACE_SUBTRACT:
  173. lhs_real -= rhs_real;
  174. lhs_imag -= rhs_imag;
  175. break;
  176. case MP_BINARY_OP_MULTIPLY:
  177. case MP_BINARY_OP_INPLACE_MULTIPLY: {
  178. mp_float_t real;
  179. multiply:
  180. real = lhs_real * rhs_real - lhs_imag * rhs_imag;
  181. lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
  182. lhs_real = real;
  183. break;
  184. }
  185. case MP_BINARY_OP_FLOOR_DIVIDE:
  186. case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
  187. mp_raise_TypeError(MP_ERROR_TEXT("can't truncate-divide a complex number"));
  188. case MP_BINARY_OP_TRUE_DIVIDE:
  189. case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
  190. if (rhs_imag == 0) {
  191. if (rhs_real == 0) {
  192. mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("complex divide by zero"));
  193. }
  194. lhs_real /= rhs_real;
  195. lhs_imag /= rhs_real;
  196. } else if (rhs_real == 0) {
  197. mp_float_t real = lhs_imag / rhs_imag;
  198. lhs_imag = -lhs_real / rhs_imag;
  199. lhs_real = real;
  200. } else {
  201. mp_float_t rhs_len_sq = rhs_real * rhs_real + rhs_imag * rhs_imag;
  202. rhs_real /= rhs_len_sq;
  203. rhs_imag /= -rhs_len_sq;
  204. goto multiply;
  205. }
  206. break;
  207. case MP_BINARY_OP_POWER:
  208. case MP_BINARY_OP_INPLACE_POWER: {
  209. // z1**z2 = exp(z2*ln(z1))
  210. // = exp(z2*(ln(|z1|)+i*arg(z1)))
  211. // = exp( (x2*ln1 - y2*arg1) + i*(y2*ln1 + x2*arg1) )
  212. // = exp(x3 + i*y3)
  213. // = exp(x3)*(cos(y3) + i*sin(y3))
  214. mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real * lhs_real + lhs_imag * lhs_imag);
  215. if (abs1 == 0) {
  216. if (rhs_imag == 0 && rhs_real >= 0) {
  217. lhs_real = (rhs_real == 0);
  218. } else {
  219. mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("0.0 to a complex power"));
  220. }
  221. } else {
  222. mp_float_t ln1 = MICROPY_FLOAT_C_FUN(log)(abs1);
  223. mp_float_t arg1 = MICROPY_FLOAT_C_FUN(atan2)(lhs_imag, lhs_real);
  224. mp_float_t x3 = rhs_real * ln1 - rhs_imag * arg1;
  225. mp_float_t y3 = rhs_imag * ln1 + rhs_real * arg1;
  226. mp_float_t exp_x3 = MICROPY_FLOAT_C_FUN(exp)(x3);
  227. lhs_real = exp_x3 * MICROPY_FLOAT_C_FUN(cos)(y3);
  228. lhs_imag = exp_x3 * MICROPY_FLOAT_C_FUN(sin)(y3);
  229. }
  230. break;
  231. }
  232. case MP_BINARY_OP_EQUAL:
  233. return mp_obj_new_bool(lhs_real == rhs_real && lhs_imag == rhs_imag);
  234. default:
  235. return MP_OBJ_NULL; // op not supported
  236. }
  237. return mp_obj_new_complex(lhs_real, lhs_imag);
  238. }
  239. #endif