blob: 3fba5689462387d15e646ce838a6a8acccf9def1 [file] [log] [blame]
Damiend99b0522013-12-21 18:17:45 +00001#include <stdlib.h>
Damiend99b0522013-12-21 18:17:45 +00002#include <assert.h>
3
4#include "nlr.h"
5#include "misc.h"
6#include "mpconfig.h"
Damien George55baff42014-01-21 21:40:13 +00007#include "qstr.h"
Damiend99b0522013-12-21 18:17:45 +00008#include "obj.h"
Damien George6e48f7f2014-03-21 11:45:46 +00009#include "parsenum.h"
Damiend99b0522013-12-21 18:17:45 +000010#include "runtime0.h"
Damiend99b0522013-12-21 18:17:45 +000011
12#if MICROPY_ENABLE_FLOAT
13
Damien George8767d072014-03-27 22:17:49 +000014#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
15#include "formatfloat.h"
16#endif
17
Damiend99b0522013-12-21 18:17:45 +000018typedef struct _mp_obj_complex_t {
19 mp_obj_base_t base;
20 mp_float_t real;
21 mp_float_t imag;
22} mp_obj_complex_t;
23
24mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
25
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020026STATIC void complex_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
Damiend99b0522013-12-21 18:17:45 +000027 mp_obj_complex_t *o = o_in;
Damien George8767d072014-03-27 22:17:49 +000028#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
29 char buf[32];
30 if (o->real == 0) {
31 format_float(o->imag, buf, sizeof(buf), 'g', 6, '\0');
Damien George6e54fcf2014-03-29 11:41:38 +000032 print(env, "%sj", buf);
Damien George8767d072014-03-27 22:17:49 +000033 } else {
34 format_float(o->real, buf, sizeof(buf), 'g', 6, '\0');
35 print(env, "(%s+", buf);
Damien George01b877d2014-03-27 23:35:31 +000036 format_float(o->imag, buf, sizeof(buf), 'g', 6, '\0');
Damien George8767d072014-03-27 22:17:49 +000037 print(env, "%sj)", buf);
38 }
39#else
Damiend99b0522013-12-21 18:17:45 +000040 if (o->real == 0) {
mux00a4da92014-01-27 10:21:42 +020041 print(env, "%.8gj", (double) o->imag);
Damiend99b0522013-12-21 18:17:45 +000042 } else {
mux00a4da92014-01-27 10:21:42 +020043 print(env, "(%.8g+%.8gj)", (double) o->real, (double) o->imag);
Damiend99b0522013-12-21 18:17:45 +000044 }
Damien George8767d072014-03-27 22:17:49 +000045#endif
Damiend99b0522013-12-21 18:17:45 +000046}
47
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020048STATIC mp_obj_t complex_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp_obj_t *args) {
Damien George20006db2014-01-18 14:10:48 +000049 // TODO check n_kw == 0
50
Damien George71c51812014-01-04 20:21:15 +000051 switch (n_args) {
52 case 0:
53 return mp_obj_new_complex(0, 0);
54
55 case 1:
Damien George6e48f7f2014-03-21 11:45:46 +000056 if (MP_OBJ_IS_STR(args[0])) {
57 // a string, parse it
58 uint l;
59 const char *s = mp_obj_str_get_data(args[0], &l);
60 return mp_parse_num_decimal(s, l, true, true);
61 } else if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
62 // a complex, just return it
Damien George71c51812014-01-04 20:21:15 +000063 return args[0];
64 } else {
Damien George6e48f7f2014-03-21 11:45:46 +000065 // something else, try to cast it to a complex
Damien George71c51812014-01-04 20:21:15 +000066 return mp_obj_new_complex(mp_obj_get_float(args[0]), 0);
67 }
68
Damien George6e48f7f2014-03-21 11:45:46 +000069 case 2: {
Damien George71c51812014-01-04 20:21:15 +000070 mp_float_t real, imag;
Damien George0c36da02014-03-08 15:24:39 +000071 if (MP_OBJ_IS_TYPE(args[0], &mp_type_complex)) {
Damien George20006db2014-01-18 14:10:48 +000072 mp_obj_complex_get(args[0], &real, &imag);
Damien George71c51812014-01-04 20:21:15 +000073 } else {
Damien George20006db2014-01-18 14:10:48 +000074 real = mp_obj_get_float(args[0]);
Damien George71c51812014-01-04 20:21:15 +000075 imag = 0;
76 }
Damien George0c36da02014-03-08 15:24:39 +000077 if (MP_OBJ_IS_TYPE(args[1], &mp_type_complex)) {
Damien George71c51812014-01-04 20:21:15 +000078 mp_float_t real2, imag2;
Damien George20006db2014-01-18 14:10:48 +000079 mp_obj_complex_get(args[1], &real2, &imag2);
Damien George71c51812014-01-04 20:21:15 +000080 real -= imag2;
81 imag += real2;
82 } else {
Damien George20006db2014-01-18 14:10:48 +000083 imag += mp_obj_get_float(args[1]);
Damien George71c51812014-01-04 20:21:15 +000084 }
85 return mp_obj_new_complex(real, imag);
86 }
87
88 default:
Damien Georgec5966122014-02-15 16:10:44 +000089 nlr_jump(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "complex takes at most 2 arguments, %d given", n_args));
Damien George71c51812014-01-04 20:21:15 +000090 }
91}
92
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +020093STATIC mp_obj_t complex_unary_op(int op, mp_obj_t o_in) {
Damiend99b0522013-12-21 18:17:45 +000094 mp_obj_complex_t *o = o_in;
95 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +010096 case MP_UNARY_OP_BOOL: return MP_BOOL(o->real != 0 || o->imag != 0);
97 case MP_UNARY_OP_POSITIVE: return o_in;
98 case MP_UNARY_OP_NEGATIVE: return mp_obj_new_complex(-o->real, -o->imag);
Damiend99b0522013-12-21 18:17:45 +000099 default: return MP_OBJ_NULL; // op not supported
100 }
101}
102
Paul Sokolovskyd5df6cd2014-02-12 18:15:40 +0200103STATIC mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
Damien Georgee2e3d112014-01-06 22:13:00 +0000104 mp_obj_complex_t *lhs = lhs_in;
105 return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
106}
107
Damien George0c36da02014-03-08 15:24:39 +0000108const mp_obj_type_t mp_type_complex = {
Damien Georgec5966122014-02-15 16:10:44 +0000109 { &mp_type_type },
Damien Georgea71c83a2014-02-15 11:34:50 +0000110 .name = MP_QSTR_complex,
Damien George97209d32014-01-07 15:58:30 +0000111 .print = complex_print,
112 .make_new = complex_make_new,
113 .unary_op = complex_unary_op,
114 .binary_op = complex_binary_op,
Damien Georgee2e3d112014-01-06 22:13:00 +0000115};
116
117mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
118 mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t);
Damien George0c36da02014-03-08 15:24:39 +0000119 o->base.type = &mp_type_complex;
Damien Georgee2e3d112014-01-06 22:13:00 +0000120 o->real = real;
121 o->imag = imag;
122 return o;
123}
124
125void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
Damien George0c36da02014-03-08 15:24:39 +0000126 assert(MP_OBJ_IS_TYPE(self_in, &mp_type_complex));
Damien Georgee2e3d112014-01-06 22:13:00 +0000127 mp_obj_complex_t *self = self_in;
128 *real = self->real;
129 *imag = self->imag;
130}
131
132mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) {
133 mp_float_t rhs_real, rhs_imag;
134 mp_obj_get_complex(rhs_in, &rhs_real, &rhs_imag); // can be any type, this function will convert to float (if possible)
Damiend99b0522013-12-21 18:17:45 +0000135 switch (op) {
Damien Georged17926d2014-03-30 13:35:08 +0100136 case MP_BINARY_OP_ADD:
137 case MP_BINARY_OP_INPLACE_ADD:
Damiend99b0522013-12-21 18:17:45 +0000138 lhs_real += rhs_real;
139 lhs_imag += rhs_imag;
140 break;
Damien Georged17926d2014-03-30 13:35:08 +0100141 case MP_BINARY_OP_SUBTRACT:
142 case MP_BINARY_OP_INPLACE_SUBTRACT:
Damiend99b0522013-12-21 18:17:45 +0000143 lhs_real -= rhs_real;
144 lhs_imag -= rhs_imag;
145 break;
Damien Georged17926d2014-03-30 13:35:08 +0100146 case MP_BINARY_OP_MULTIPLY:
147 case MP_BINARY_OP_INPLACE_MULTIPLY:
Damiend99b0522013-12-21 18:17:45 +0000148 {
149 mp_float_t real = lhs_real * rhs_real - lhs_imag * rhs_imag;
150 lhs_imag = lhs_real * rhs_imag + lhs_imag * rhs_real;
151 lhs_real = real;
152 break;
153 }
154 /* TODO floor(?) the value
Damien Georged17926d2014-03-30 13:35:08 +0100155 case MP_BINARY_OP_FLOOR_DIVIDE:
156 case MP_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break;
Damiend99b0522013-12-21 18:17:45 +0000157 */
158 /* TODO
Damien Georged17926d2014-03-30 13:35:08 +0100159 case MP_BINARY_OP_TRUE_DIVIDE:
160 case MP_BINARY_OP_INPLACE_TRUE_DIVIDE: val = lhs_val / rhs_val; break;
Damiend99b0522013-12-21 18:17:45 +0000161 */
162 return NULL; // op not supported
163 }
164 return mp_obj_new_complex(lhs_real, lhs_imag);
165}
166
Damiend99b0522013-12-21 18:17:45 +0000167#endif