objcomplex.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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: return mp_obj_new_bool(o->real != 0 || o->imag != 0);
  110. case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT(mp_float_hash(o->real) ^ mp_float_hash(o->imag));
  111. case MP_UNARY_OP_POSITIVE: return o_in;
  112. case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
  113. case MP_UNARY_OP_ABS:
  114. return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(o->real*o->real + o->imag*o->imag));
  115. default: return MP_OBJ_NULL; // op not supported
  116. }
  117. }
  118. STATIC mp_obj_t complex_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
  119. mp_obj_complex_t *lhs = MP_OBJ_TO_PTR(lhs_in);
  120. return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
  121. }
  122. STATIC void complex_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
  123. if (dest[0] != MP_OBJ_NULL) {
  124. // not load attribute
  125. return;
  126. }
  127. mp_obj_complex_t *self = MP_OBJ_TO_PTR(self_in);
  128. if (attr == MP_QSTR_real) {
  129. dest[0] = mp_obj_new_float(self->real);
  130. } else if (attr == MP_QSTR_imag) {
  131. dest[0] = mp_obj_new_float(self->imag);
  132. }
  133. }
  134. const mp_obj_type_t mp_type_complex = {
  135. { &mp_type_type },
  136. .name = MP_QSTR_complex,
  137. .print = complex_print,
  138. .make_new = complex_make_new,
  139. .unary_op = complex_unary_op,
  140. .binary_op = complex_binary_op,
  141. .attr = complex_attr,
  142. };
  143. mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
  144. mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t);
  145. o->base.type = &mp_type_complex;
  146. o->real = real;
  147. o->imag = imag;
  148. return MP_OBJ_FROM_PTR(o);
  149. }
  150. void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
  151. assert(MP_OBJ_IS_TYPE(self_in, &mp_type_complex));
  152. mp_obj_complex_t *self = MP_OBJ_TO_PTR(self_in);
  153. *real = self->real;
  154. *imag = self->imag;
  155. }
  156. 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) {
  157. mp_float_t rhs_real, rhs_imag;
  158. mp_obj_get_complex(rhs_in, &rhs_real, &rhs_imag); // can be any type, this function will convert to float (if possible)
  159. switch (op) {
  160. case MP_BINARY_OP_ADD:
  161. case MP_BINARY_OP_INPLACE_ADD:
  162. lhs_real += rhs_real;
  163. lhs_imag += rhs_imag;
  164. break;
  165. case MP_BINARY_OP_SUBTRACT:
  166. case MP_BINARY_OP_INPLACE_SUBTRACT:
  167. lhs_real -= rhs_real;
  168. lhs_imag -= rhs_imag;
  169. break;
  170. case MP_BINARY_OP_MULTIPLY:
  171. case MP_BINARY_OP_INPLACE_MULTIPLY: {
  172. mp_float_t real;
  173. multiply:
  174. real = lhs_real * rhs_real - lhs_imag * rhs_imag;
  175. lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
  176. lhs_real = real;
  177. break;
  178. }
  179. case MP_BINARY_OP_FLOOR_DIVIDE:
  180. case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE:
  181. mp_raise_TypeError("can't do truncated division of a complex number");
  182. case MP_BINARY_OP_TRUE_DIVIDE:
  183. case MP_BINARY_OP_INPLACE_TRUE_DIVIDE:
  184. if (rhs_imag == 0) {
  185. if (rhs_real == 0) {
  186. mp_raise_msg(&mp_type_ZeroDivisionError, "complex division by zero");
  187. }
  188. lhs_real /= rhs_real;
  189. lhs_imag /= rhs_real;
  190. } else if (rhs_real == 0) {
  191. mp_float_t real = lhs_imag / rhs_imag;
  192. lhs_imag = -lhs_real / rhs_imag;
  193. lhs_real = real;
  194. } else {
  195. mp_float_t rhs_len_sq = rhs_real*rhs_real + rhs_imag*rhs_imag;
  196. rhs_real /= rhs_len_sq;
  197. rhs_imag /= -rhs_len_sq;
  198. goto multiply;
  199. }
  200. break;
  201. case MP_BINARY_OP_POWER:
  202. case MP_BINARY_OP_INPLACE_POWER: {
  203. // z1**z2 = exp(z2*ln(z1))
  204. // = exp(z2*(ln(|z1|)+i*arg(z1)))
  205. // = exp( (x2*ln1 - y2*arg1) + i*(y2*ln1 + x2*arg1) )
  206. // = exp(x3 + i*y3)
  207. // = exp(x3)*(cos(y3) + i*sin(y3))
  208. mp_float_t abs1 = MICROPY_FLOAT_C_FUN(sqrt)(lhs_real*lhs_real + lhs_imag*lhs_imag);
  209. if (abs1 == 0) {
  210. if (rhs_imag == 0 && rhs_real >= 0) {
  211. lhs_real = (rhs_real == 0);
  212. } else {
  213. mp_raise_msg(&mp_type_ZeroDivisionError, "0.0 to a complex power");
  214. }
  215. } else {
  216. mp_float_t ln1 = MICROPY_FLOAT_C_FUN(log)(abs1);
  217. mp_float_t arg1 = MICROPY_FLOAT_C_FUN(atan2)(lhs_imag, lhs_real);
  218. mp_float_t x3 = rhs_real * ln1 - rhs_imag * arg1;
  219. mp_float_t y3 = rhs_imag * ln1 + rhs_real * arg1;
  220. mp_float_t exp_x3 = MICROPY_FLOAT_C_FUN(exp)(x3);
  221. lhs_real = exp_x3 * MICROPY_FLOAT_C_FUN(cos)(y3);
  222. lhs_imag = exp_x3 * MICROPY_FLOAT_C_FUN(sin)(y3);
  223. }
  224. break;
  225. }
  226. case MP_BINARY_OP_EQUAL: return mp_obj_new_bool(lhs_real == rhs_real && lhs_imag == rhs_imag);
  227. default:
  228. return MP_OBJ_NULL; // op not supported
  229. }
  230. return mp_obj_new_complex(lhs_real, lhs_imag);
  231. }
  232. #endif