objcomplex.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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_decimal(s, l, true, true, 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. // something else, try to cast it to a complex
  83. return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
  84. }
  85. case 2:
  86. default: {
  87. mp_float_t real, imag;
  88. if (mp_obj_is_type(args[0], &mp_type_complex)) {
  89. mp_obj_complex_get(args[0], &real, &imag);
  90. } else {
  91. real = mp_obj_get_float(args[0]);
  92. imag = 0;
  93. }
  94. if (mp_obj_is_type(args[1], &mp_type_complex)) {
  95. mp_float_t real2, imag2;
  96. mp_obj_complex_get(args[1], &real2, &imag2);
  97. real -= imag2;
  98. imag += real2;
  99. } else {
  100. imag += mp_obj_get_float(args[1]);
  101. }
  102. return mp_obj_new_complex(real, imag);
  103. }
  104. }
  105. }
  106. STATIC mp_obj_t complex_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
  107. mp_obj_complex_t *o = MP_OBJ_TO_PTR(o_in);
  108. switch (op) {
  109. case MP_UNARY_OP_BOOL:
  110. return mp_obj_new_bool(o->real != 0 || o->imag != 0);
  111. case MP_UNARY_OP_HASH:
  112. return MP_OBJ_NEW_SMALL_INT(mp_float_hash(o->real) ^ mp_float_hash(o->imag));
  113. case MP_UNARY_OP_POSITIVE:
  114. return o_in;
  115. case MP_UNARY_OP_NEGATIVE:
  116. return mp_obj_new_complex(-o->real, -o->imag);
  117. case MP_UNARY_OP_ABS:
  118. return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(o->real * o->real + o->imag * o->imag));
  119. default:
  120. return MP_OBJ_NULL; // op not supported
  121. }
  122. }
  123. STATIC mp_obj_t complex_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
  124. mp_obj_complex_t *lhs = MP_OBJ_TO_PTR(lhs_in);
  125. return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
  126. }
  127. STATIC void complex_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  128. if (dest[0] != MP_OBJ_NULL) {
  129. // not load attribute
  130. return;
  131. }
  132. mp_obj_complex_t *self = MP_OBJ_TO_PTR(self_in);
  133. if (attr == MP_QSTR_real) {
  134. dest[0] = mp_obj_new_float(self->real);
  135. } else if (attr == MP_QSTR_imag) {
  136. dest[0] = mp_obj_new_float(self->imag);
  137. }
  138. }
  139. const mp_obj_type_t mp_type_complex = {
  140. { &mp_type_type },
  141. .flags = MP_TYPE_FLAG_EQ_NOT_REFLEXIVE | MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE,
  142. .name = MP_QSTR_complex,
  143. .print = complex_print,
  144. .make_new = complex_make_new,
  145. .unary_op = complex_unary_op,
  146. .binary_op = complex_binary_op,
  147. .attr = complex_attr,
  148. };
  149. mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
  150. mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t);
  151. o->base.type = &mp_type_complex;
  152. o->real = real;
  153. o->imag = imag;
  154. return MP_OBJ_FROM_PTR(o);
  155. }
  156. void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
  157. assert(mp_obj_is_type(self_in, &mp_type_complex));
  158. mp_obj_complex_t *self = MP_OBJ_TO_PTR(self_in);
  159. *real = self->real;
  160. *imag = self->imag;
  161. }
  162. 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) {
  163. mp_float_t rhs_real, rhs_imag;
  164. if (!mp_obj_get_complex_maybe(rhs_in, &rhs_real, &rhs_imag)) {
  165. return MP_OBJ_NULL; // op not supported
  166. }
  167. switch (op) {
  168. case MP_BINARY_OP_ADD:
  169. case MP_BINARY_OP_INPLACE_ADD:
  170. lhs_real += rhs_real;
  171. lhs_imag += rhs_imag;
  172. break;
  173. case MP_BINARY_OP_SUBTRACT:
  174. case MP_BINARY_OP_INPLACE_SUBTRACT:
  175. lhs_real -= rhs_real;
  176. lhs_imag -= rhs_imag;
  177. break;
  178. case MP_BINARY_OP_MULTIPLY:
  179. case MP_BINARY_OP_INPLACE_MULTIPLY: {
  180. mp_float_t real;
  181. multiply:
  182. real = lhs_real * rhs_real - lhs_imag * rhs_imag;
  183. lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
  184. lhs_real = real;
  185. break;
  186. }
  187. case MP_BINARY_OP_FLOOR_DIVIDE:
  188. case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
  189. mp_raise_TypeError(MP_ERROR_TEXT("can't truncate-divide a complex number"));
  190. case MP_BINARY_OP_TRUE_DIVIDE:
  191. case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
  192. if (rhs_imag == 0) {
  193. if (rhs_real == 0) {
  194. mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("complex divide by zero"));
  195. }
  196. lhs_real /= rhs_real;
  197. lhs_imag /= rhs_real;
  198. } else if (rhs_real == 0) {
  199. mp_float_t real = lhs_imag / rhs_imag;
  200. lhs_imag = -lhs_real / rhs_imag;
  201. lhs_real = real;
  202. } else {
  203. mp_float_t rhs_len_sq = rhs_real * rhs_real + rhs_imag * rhs_imag;
  204. rhs_real /= rhs_len_sq;
  205. rhs_imag /= -rhs_len_sq;
  206. goto multiply;
  207. }
  208. break;
  209. case MP_BINARY_OP_POWER:
  210. case MP_BINARY_OP_INPLACE_POWER: {
  211. // z1**z2 = exp(z2*ln(z1))
  212. // = exp(z2*(ln(|z1|)+i*arg(z1)))
  213. // = exp( (x2*ln1 - y2*arg1) + i*(y2*ln1 + x2*arg1) )
  214. // = exp(x3 + i*y3)
  215. // = exp(x3)*(cos(y3) + i*sin(y3))
  216. mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real * lhs_real + lhs_imag * lhs_imag);
  217. if (abs1 == 0) {
  218. if (rhs_imag == 0 && rhs_real >= 0) {
  219. lhs_real = (rhs_real == 0);
  220. } else {
  221. mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("0.0 to a complex power"));
  222. }
  223. } else {
  224. mp_float_t ln1 = MICROPY_FLOAT_C_FUN(log)(abs1);
  225. mp_float_t arg1 = MICROPY_FLOAT_C_FUN(atan2)(lhs_imag, lhs_real);
  226. mp_float_t x3 = rhs_real * ln1 - rhs_imag * arg1;
  227. mp_float_t y3 = rhs_imag * ln1 + rhs_real * arg1;
  228. mp_float_t exp_x3 = MICROPY_FLOAT_C_FUN(exp)(x3);
  229. lhs_real = exp_x3 * MICROPY_FLOAT_C_FUN(cos)(y3);
  230. lhs_imag = exp_x3 * MICROPY_FLOAT_C_FUN(sin)(y3);
  231. }
  232. break;
  233. }
  234. case MP_BINARY_OP_EQUAL:
  235. return mp_obj_new_bool(lhs_real == rhs_real && lhs_imag == rhs_imag);
  236. default:
  237. return MP_OBJ_NULL; // op not supported
  238. }
  239. return mp_obj_new_complex(lhs_real, lhs_imag);
  240. }
  241. #endif