Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <stdint.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | |
| 6 | #include "nlr.h" |
| 7 | #include "misc.h" |
| 8 | #include "mpconfig.h" |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 9 | #include "mpqstr.h" |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 10 | #include "obj.h" |
| 11 | #include "runtime0.h" |
| 12 | #include "map.h" |
| 13 | |
| 14 | #if MICROPY_ENABLE_FLOAT |
| 15 | |
| 16 | typedef struct _mp_obj_complex_t { |
| 17 | mp_obj_base_t base; |
| 18 | mp_float_t real; |
| 19 | mp_float_t imag; |
| 20 | } mp_obj_complex_t; |
| 21 | |
| 22 | mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag); |
| 23 | |
| 24 | void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in) { |
| 25 | mp_obj_complex_t *o = o_in; |
| 26 | if (o->real == 0) { |
| 27 | print(env, "%.8gj", o->imag); |
| 28 | } else { |
| 29 | print(env, "(%.8g+%.8gj)", o->real, o->imag); |
| 30 | } |
| 31 | } |
| 32 | |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 33 | // args are reverse in the array |
| 34 | static mp_obj_t complex_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) { |
| 35 | switch (n_args) { |
| 36 | case 0: |
| 37 | return mp_obj_new_complex(0, 0); |
| 38 | |
| 39 | case 1: |
| 40 | // TODO allow string as first arg and parse it |
| 41 | if (MP_OBJ_IS_TYPE(args[0], &complex_type)) { |
| 42 | return args[0]; |
| 43 | } else { |
| 44 | return mp_obj_new_complex(mp_obj_get_float(args[0]), 0); |
| 45 | } |
| 46 | |
| 47 | case 2: |
| 48 | { |
| 49 | mp_float_t real, imag; |
| 50 | if (MP_OBJ_IS_TYPE(args[1], &complex_type)) { |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 51 | mp_obj_complex_get(args[1], &real, &imag); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 52 | } else { |
| 53 | real = mp_obj_get_float(args[1]); |
| 54 | imag = 0; |
| 55 | } |
| 56 | if (MP_OBJ_IS_TYPE(args[0], &complex_type)) { |
| 57 | mp_float_t real2, imag2; |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 58 | mp_obj_complex_get(args[0], &real2, &imag2); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 59 | real -= imag2; |
| 60 | imag += real2; |
| 61 | } else { |
| 62 | imag += mp_obj_get_float(args[0]); |
| 63 | } |
| 64 | return mp_obj_new_complex(real, imag); |
| 65 | } |
| 66 | |
| 67 | default: |
| 68 | nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "complex takes at most 2 arguments, %d given", (void*)(machine_int_t)n_args)); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | static mp_obj_t complex_unary_op(int op, mp_obj_t o_in) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 73 | mp_obj_complex_t *o = o_in; |
| 74 | switch (op) { |
| 75 | case RT_UNARY_OP_NOT: if (o->real != 0 || o->imag != 0) { return mp_const_true;} else { return mp_const_false; } |
| 76 | case RT_UNARY_OP_POSITIVE: return o_in; |
| 77 | case RT_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag); |
| 78 | default: return MP_OBJ_NULL; // op not supported |
| 79 | } |
| 80 | } |
| 81 | |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 82 | static mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 83 | mp_obj_complex_t *lhs = lhs_in; |
| 84 | return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in); |
| 85 | } |
| 86 | |
| 87 | const mp_obj_type_t complex_type = { |
| 88 | { &mp_const_type }, |
| 89 | "complex", |
| 90 | complex_print, // print |
| 91 | complex_make_new, // make_new |
| 92 | NULL, // call_n |
| 93 | complex_unary_op, // unary_op |
| 94 | complex_binary_op, // binary_op |
| 95 | NULL, // getiter |
| 96 | NULL, // iternext |
| 97 | .methods = { { NULL, NULL }, }, |
| 98 | }; |
| 99 | |
| 100 | mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) { |
| 101 | mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t); |
| 102 | o->base.type = &complex_type; |
| 103 | o->real = real; |
| 104 | o->imag = imag; |
| 105 | return o; |
| 106 | } |
| 107 | |
| 108 | void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) { |
| 109 | assert(MP_OBJ_IS_TYPE(self_in, &complex_type)); |
| 110 | mp_obj_complex_t *self = self_in; |
| 111 | *real = self->real; |
| 112 | *imag = self->imag; |
| 113 | } |
| 114 | |
| 115 | mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) { |
| 116 | mp_float_t rhs_real, rhs_imag; |
| 117 | mp_obj_get_complex(rhs_in, &rhs_real, &rhs_imag); // can be any type, this function will convert to float (if possible) |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 118 | switch (op) { |
| 119 | case RT_BINARY_OP_ADD: |
| 120 | case RT_BINARY_OP_INPLACE_ADD: |
| 121 | lhs_real += rhs_real; |
| 122 | lhs_imag += rhs_imag; |
| 123 | break; |
| 124 | case RT_BINARY_OP_SUBTRACT: |
| 125 | case RT_BINARY_OP_INPLACE_SUBTRACT: |
| 126 | lhs_real -= rhs_real; |
| 127 | lhs_imag -= rhs_imag; |
| 128 | break; |
| 129 | case RT_BINARY_OP_MULTIPLY: |
| 130 | case RT_BINARY_OP_INPLACE_MULTIPLY: |
| 131 | { |
| 132 | mp_float_t real = lhs_real * rhs_real - lhs_imag * rhs_imag; |
| 133 | lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real; |
| 134 | lhs_real = real; |
| 135 | break; |
| 136 | } |
| 137 | /* TODO floor(?) the value |
| 138 | case RT_BINARY_OP_FLOOR_DIVIDE: |
| 139 | case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break; |
| 140 | */ |
| 141 | /* TODO |
| 142 | case RT_BINARY_OP_TRUE_DIVIDE: |
| 143 | case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: val = lhs_val / rhs_val; break; |
| 144 | */ |
| 145 | return NULL; // op not supported |
| 146 | } |
| 147 | return mp_obj_new_complex(lhs_real, lhs_imag); |
| 148 | } |
| 149 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 150 | #endif |