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 | |
Paul Sokolovsky | 76d982e | 2014-01-13 19:19:16 +0200 | [diff] [blame] | 24 | void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 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 | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 33 | static mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) { |
| 34 | // TODO check n_kw == 0 |
| 35 | |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 36 | switch (n_args) { |
| 37 | case 0: |
| 38 | return mp_obj_new_complex(0, 0); |
| 39 | |
| 40 | case 1: |
| 41 | // TODO allow string as first arg and parse it |
| 42 | if (MP_OBJ_IS_TYPE(args[0], &complex_type)) { |
| 43 | return args[0]; |
| 44 | } else { |
| 45 | return mp_obj_new_complex(mp_obj_get_float(args[0]), 0); |
| 46 | } |
| 47 | |
| 48 | case 2: |
| 49 | { |
| 50 | mp_float_t real, imag; |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 51 | if (MP_OBJ_IS_TYPE(args[0], &complex_type)) { |
| 52 | mp_obj_complex_get(args[0], &real, &imag); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 53 | } else { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 54 | real = mp_obj_get_float(args[0]); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 55 | imag = 0; |
| 56 | } |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 57 | if (MP_OBJ_IS_TYPE(args[1], &complex_type)) { |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 58 | mp_float_t real2, imag2; |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 59 | mp_obj_complex_get(args[1], &real2, &imag2); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 60 | real -= imag2; |
| 61 | imag += real2; |
| 62 | } else { |
Damien George | 20006db | 2014-01-18 14:10:48 +0000 | [diff] [blame] | 63 | imag += mp_obj_get_float(args[1]); |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 64 | } |
| 65 | return mp_obj_new_complex(real, imag); |
| 66 | } |
| 67 | |
| 68 | default: |
| 69 | 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)); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | 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] | 74 | mp_obj_complex_t *o = o_in; |
| 75 | switch (op) { |
| 76 | case RT_UNARY_OP_NOT: if (o->real != 0 || o->imag != 0) { return mp_const_true;} else { return mp_const_false; } |
| 77 | case RT_UNARY_OP_POSITIVE: return o_in; |
| 78 | case RT_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag); |
| 79 | default: return MP_OBJ_NULL; // op not supported |
| 80 | } |
| 81 | } |
| 82 | |
Damien George | 71c5181 | 2014-01-04 20:21:15 +0000 | [diff] [blame] | 83 | 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] | 84 | mp_obj_complex_t *lhs = lhs_in; |
| 85 | return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in); |
| 86 | } |
| 87 | |
| 88 | const mp_obj_type_t complex_type = { |
| 89 | { &mp_const_type }, |
| 90 | "complex", |
Damien George | 97209d3 | 2014-01-07 15:58:30 +0000 | [diff] [blame] | 91 | .print = complex_print, |
| 92 | .make_new = complex_make_new, |
| 93 | .unary_op = complex_unary_op, |
| 94 | .binary_op = complex_binary_op, |
Damien George | e2e3d11 | 2014-01-06 22:13:00 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) { |
| 98 | mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t); |
| 99 | o->base.type = &complex_type; |
| 100 | o->real = real; |
| 101 | o->imag = imag; |
| 102 | return o; |
| 103 | } |
| 104 | |
| 105 | void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) { |
| 106 | assert(MP_OBJ_IS_TYPE(self_in, &complex_type)); |
| 107 | mp_obj_complex_t *self = self_in; |
| 108 | *real = self->real; |
| 109 | *imag = self->imag; |
| 110 | } |
| 111 | |
| 112 | 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) { |
| 113 | mp_float_t rhs_real, rhs_imag; |
| 114 | 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] | 115 | switch (op) { |
| 116 | case RT_BINARY_OP_ADD: |
| 117 | case RT_BINARY_OP_INPLACE_ADD: |
| 118 | lhs_real += rhs_real; |
| 119 | lhs_imag += rhs_imag; |
| 120 | break; |
| 121 | case RT_BINARY_OP_SUBTRACT: |
| 122 | case RT_BINARY_OP_INPLACE_SUBTRACT: |
| 123 | lhs_real -= rhs_real; |
| 124 | lhs_imag -= rhs_imag; |
| 125 | break; |
| 126 | case RT_BINARY_OP_MULTIPLY: |
| 127 | case RT_BINARY_OP_INPLACE_MULTIPLY: |
| 128 | { |
| 129 | mp_float_t real = lhs_real * rhs_real - lhs_imag * rhs_imag; |
| 130 | lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real; |
| 131 | lhs_real = real; |
| 132 | break; |
| 133 | } |
| 134 | /* TODO floor(?) the value |
| 135 | case RT_BINARY_OP_FLOOR_DIVIDE: |
| 136 | case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break; |
| 137 | */ |
| 138 | /* TODO |
| 139 | case RT_BINARY_OP_TRUE_DIVIDE: |
| 140 | case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: val = lhs_val / rhs_val; break; |
| 141 | */ |
| 142 | return NULL; // op not supported |
| 143 | } |
| 144 | return mp_obj_new_complex(lhs_real, lhs_imag); |
| 145 | } |
| 146 | |
Damien | d99b052 | 2013-12-21 18:17:45 +0000 | [diff] [blame] | 147 | #endif |